<?php

class hisql {

    static public function db($host, $username, $password, $port, $dbname, $charset) {
        $db = array(
            "host" => $host,
            "username" => $username,
            "password" => $password,
            "port" => $port,
            "dbname" => $dbname,
            "charset" => $charset
        );
        $hisql = new \Phalcon\Db\Adapter\Pdo\Mysql($db);
        hilog::info("db: " . json_encode($db));
        return $hisql;
    }

    static public function query($db, $q, $selectlist) {
        hilog::info("q: " . $q);
        $rows = $db->fetchAll($q, Phalcon\Db::FETCH_ASSOC);
        $result = [];
        foreach ($rows as $row) {
            $result[] = common::sql_row($selectlist, $row);
        }
        return $result;
    }

    static public function count($db, $q) {
        return $db->fetchColumn($q);
    }

    static public function insert($db, $table, $values, $fields) {
        return $db->insert($table, $values, $fields);
    }

}
